package basealgorithm;

import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Scanner;

/**
 * @author : sicaolong
 * @description :
 * @date : 2021/2/15  11:06 下午
 */
public class MergeSort {
    private static int n;
    private static int[] nums;
    private static int[] temp;

    public static void main(String[] args) {
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        n = scan.nextInt();
        nums = new int[n];
        temp = new int[n];

        for (int i = 0; i < n; i++) {
            nums[i] = scan.nextInt();
        }
        mergeSort(nums, 0, n - 1);
        Arrays.stream(nums).forEach(s -> System.out.print(s + " "));
    }

    public static void mergeSort(int[] nums, int l, int r) {
        if (l >= r) {
            return;
        }
        int mid = l + r >> 1;
        //1、递归分治
        mergeSort(nums, l, mid);
        mergeSort(nums, mid + 1, r);

        //2、合并
        int k = 0;
        int index1 = l;
        int index2 = mid + 1;
        //2.1 将数从小到大 放置到临时数组 temp中
        while (index1 <= mid && index2 <= r) {
            if (nums[index1] < nums[index2]) temp[k++] = nums[index1++];
            else temp[k++] = nums[index2++];
        }
        while (index1 <= mid) temp[k++] = nums[index1++];
        while (index2 <= r) temp[k++] = nums[index2++];

        //2.2 从临时数组 将数字 复制回nums数组中
        for (int i = l, j = 0; i <= r; i++, j++) {
            nums[i] = temp[j];
        }

    }
}